package com.sw.controller;

import com.sw.pojo.Result;
import com.sw.pojo.User;
import com.sw.service.UserService;
import com.sw.utils.JwtUtil;
import com.sw.utils.Md5Util;
import com.sw.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        //查询用户
        User u = userService.findByUsername(username);
        if (u == null) {
            userService.register(username, password);
            System.out.println("注册成功");
            return Result.success();
        } else {
            return Result.error("用户已存在");
        }
    }

    @GetMapping("/login")
    public Result login(String username, String password) {
        User u = userService.findByUsername(username);
        if (u == null) {
            return Result.error("用户不存在");
        } else {
            if (Md5Util.getMD5String(password).equals(u.getPassword())) {
                Map<String, Object> claims = new HashMap<>();
                claims.put("username", u.getUsername());
                claims.put("id", u.getId());
                String token = JwtUtil.genToken(claims);
                return Result.success(token);
            } else {
                return Result.error("密码错误");
            }
        }
    }

    @GetMapping("/userInfo")
    public Result<User> userInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User userInfo = userService.findByUsername(username);
        return Result.success(userInfo);
    }

    @PutMapping("/update")
    public Result update(@RequestBody @Validated User user) {
        System.out.println(user);
        userService.update(user);
        return Result.success();
    }

    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam @URL String avatarUrl) {
        userService.updateAvatar(avatarUrl);
        return Result.success();
    }

    @PatchMapping("/resetPassword")
    public Result resetPassword(@RequestBody Map<String, String> params) {
        //验证旧密码是否正确
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");
        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error("缺少必要参数");
        }

        Map<String, Object> map = ThreadLocalUtil.get();
        if (!Md5Util.getMD5String(oldPwd).equals(userService.findByUsername((String) map.get("username")).getPassword())) {
            return Result.error("旧密码不正确");
        }

        if (oldPwd.equals(newPwd)) {
            return Result.error("新旧密码不能相同");
        }

        if (newPwd.length() < 5 || newPwd.length() > 16) {
            return Result.error("密码长度在5-16位之间");
        }

        if (!newPwd.equals(rePwd)) {
            return Result.error("两次密码不一致");
        }
        userService.updatePwd(newPwd);
        return Result.success();
    }
}
